import { ValidationPipe } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';

import { AppModule } from './app.module';
import { InvokeRecordInterceptor } from './invoke-record.interceptor';

async function bootstrap() {
    const app = await NestFactory.create(AppModule);
    app.useGlobalPipes(
        new ValidationPipe({
            transform: true,
            whitelist: true,
            forbidNonWhitelisted: true,
            forbidUnknownValues: true,
            validationError: { target: false },
        }),
    );
    app.useGlobalInterceptors(new InvokeRecordInterceptor());
    const config = new DocumentBuilder()
        .setTitle('会议室预订系统')
        .setDescription('api 接口文档')
        .setVersion('1.0')
        .build();
    const document = SwaggerModule.createDocument(app, config);
    // 修改swagger文档中post请求响应状态码201为200
    // document.paths = Object.fromEntries(
    //     Object.entries(document.paths).map(([path, methods]) => [
    //         path,
    //         Object.fromEntries(
    //             Object.entries(methods).map(([method, details]) => [
    //                 method,
    //                 {
    //                     ...details,
    //                     responses: {
    //                         200: { description: 'OK' },
    //                     },
    //                 },
    //             ]),
    //         ),
    //     ]),
    // );

    SwaggerModule.setup('api-doc', app, document);
    app.enableCors();
    const configService = app.get(ConfigService);
    await app.listen(configService.get('nest_server_port'));
}
bootstrap();
